<?
require_once('../lib/Config.php');
require_once('../lib/DB.gear.php');
require_once('../lib/lib.gear.php');
require_once('../lib/lib.garden.php');

if(isset($_POST['g_key_din39s03j23ogn']) && $_POST['g_key_din39s03j23ogn'] == G_KEY)
{
    $user_id    = $_POST['user_id'];
    $startIndex = 0;
	$count      = 10;

	if(isset($_POST['count']) && ($_POST['count'] > 0))
	{
		$count = $_POST['count'];
	}

	if(isset($_POST['startIndex']) && ($_POST['startIndex'] >= 0))
	{
	    $startIndex = $_POST['startIndex'] * $count;
	}

    $db = new DB_GEAR();

    //set query
    $aQuery['Type']   = 'direct';
    $aQuery['Query']  = 'select user.user_id from garden_user_tbl as user inner join garden_mixi_tbl as mixi on user.user_id = mixi.using_flag where mixi.user_id = '.$user_id;

    //set query
    $db->set_query($aQuery);
    //echo $db->get_query();
    //send query
    $result = $db->send_query();
    $comma = '';
	if(mysql_num_rows($result))
	{
		while($rec = mysql_fetch_array($result,MYSQL_ASSOC))
		{
			$ids[] = $rec['user_id'];
		}
	}
	$ids[] = $user_id;

	$user_id_set = implode(",", $ids);

	$db->clear_query();

    //set query
    $aQuery['Type']   = 'direct';
    $aQuery['Query']  = 'select pod.pod_id from garden_pod_members_tbl as member inner join garden_pod_tbl as pod on member.pod_id = pod.pod_id where member.user_id = '.$user_id.' ORDER BY pod.pod_id DESC';

    //set query
    $db->set_query($aQuery);
    //send query
    $result = $db->send_query(1);

	if(is_null($result) == false){
		while($rec = mysql_fetch_array($result,MYSQL_ASSOC))
		{
			$pod_ids[] = $rec['pod_id'];
		}
	}
	else
	{
		$pod_ids[] = '-1';
	}

	$pod_id_set = implode(",", $pod_ids);

	//preDump($pod_id_set);

	$db->clear_query();

	//set query
    $aQuery['Type']   = 'direct';
    $aQuery['Query']  = 'SELECT * FROM garden_contents_tbl as contents left join garden_user_tbl as user on contents.user_id = user.user_id WHERE pod_id IN ('.$pod_id_set.') or ( contents.user_id IN ('.$user_id_set.') and contents.pod_id = 0) ORDER BY time_stamp DESC LIMIT '.$startIndex.', '.$count.'';

    //set query
    $db->set_query($aQuery);
    
    //send query
    $result = $db->send_query(1, 1, 1);

	if($result != false){
		while($rec = mysql_fetch_array($result,MYSQL_ASSOC))
		{
			$data[] = $rec;
		}
	}
	else
	{
	    $data = array(
	                   'status'  => '0',
	                  );
	}
	//$data[] = $db->get_query();

    $json_data = json_encode($data);
    
    echo $json_data;


}
else
{

}

?>
